package services;

import tw.edu.tku.im.maze.DeliveryListenerI;
import tw.edu.tku.im.maze.RepPacket;
import tw.edu.tku.im.maze.StartGameDeliverer;
import tw.edu.tku.im.maze.StartGameReq;

import com.example.maze.ConnectionInfo;

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

public class StartGameService extends    IntentService 
                              implements DeliveryListenerI{
   public static final String BROADCAST_ACTION= "StartGameService";

   public StartGameService()            { super("StartGameService"); }
   public StartGameService(String name) { super(name); }

   @Override
   protected void onHandleIntent(Intent intent) {
      try {
         StartGameDeliverer mSatrtGameDeliverer= new StartGameDeliverer();
         mSatrtGameDeliverer.deliver(new StartGameReq(ConnectionInfo.roomId,
                                                      ConnectionInfo.id,
                                                      ConnectionInfo.ticket), 
                                                      this);
      } catch(Exception e) {} 
   }

   @Override
   public void processReply(RepPacket rep) {
      Intent localIntent= new Intent(BROADCAST_ACTION);
      localIntent.putExtra("packet", rep);
      LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
   }
}
